package cn.jinbyte.web.utils.helper;

import cn.jinbyte.core.exception.E401;
import cn.jinbyte.core.utils.ServletUtils;
import cn.jinbyte.web.config.WebConst;
import cn.jinbyte.web.model.AccessUser;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.ContentCachingRequestWrapper;

import java.io.BufferedReader;

import static cn.jinbyte.web.config.WebConst.ATTR_USER_KEY;

/**
 * 请求工具类
 */
@Slf4j
public class WebRequestHelper {
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 获取session
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取参数
     */
    public static String getParameter(String name) {
        return getRequest().getParameter(name);
    }

    /**
     * 获取请求体参数
     */
    public static String getRequestBody(ServletRequest request) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String data;
            while ((data = reader.readLine()) != null) {
                sb.append(data);
            }
            return sb.toString();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 获取请求头参数
     */
    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    /**
     * 获取token
     */
    public static String token() {
        String token = getRequest().getHeader(WebConst.TOKEN_KEY);
        if (token == null) {
            throw new E401("token认证失败");
        }
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        return token;
    }

    /**
     * 获取请求体数据
     *
     * @param request HttpServletRequest对象
     * @return 请求体数据
     */
    public static String getBody(HttpServletRequest request) {
        if (request instanceof ContentCachingRequestWrapper) {
            if (!ServletUtils.isMultipartContent(request)) {
                // todo
                return null;
            }
        }
        return null;
    }


    /**
     * 将用户信息存入请求属性
     */
    public static void setCurrentUser(HttpServletRequest request, AccessUser accessUser) {
        request.setAttribute(ATTR_USER_KEY, accessUser);
    }

    /**
     * 从请求属性中获取用户信息
     */
    public static AccessUser getCurrentUser(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        return (AccessUser) request.getAttribute(ATTR_USER_KEY);
    }
}
